package com.kgc.controller;


import com.kgc.annotation.CheckPermission;
import com.kgc.entity.News;
import com.kgc.service.NewsService;
import com.kgc.util.ResultAjax;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/news")
public class NewsController {


    @Autowired
    private NewsService newsService;


    @RequestMapping("/page")
    public ResultAjax NewsPageInfo(@RequestBody Map<String, Object> params) {
        int pageNum = (int) params.get("pageNum");
        int pageSize = (int) params.get("pageSize");
        String searchTitle = (String) params.get("searchTitle");
        String orderBy = (String) params.get("orderBy");
        Map<String, Object> paramsPage = new HashMap<>();
        if (!StringUtils.isEmpty(searchTitle)) {
            paramsPage.put("searchTitle", searchTitle);
        }
        paramsPage.put("orderBy", orderBy);
        paramsPage.put("pageNum", pageNum);
        paramsPage.put("pageSize", pageSize);
        return ResultAjax.success().add("pageInfo", newsService.selectNewsByTitle(paramsPage));
    }

    @RequestMapping("/addNews")
    @CheckPermission({"1"})
    public ResultAjax addNews(@RequestBody Map<String, Object> params) {
        String title = (String) params.get("title");
        String content = (String) params.get("content");
        News news = new News();
        news.setTitle(title);
        news.setContent(content);
        news.setCreateTime(new Date());
        news.setIsDelete(0);
        if (newsService.addNews(news) > 0) {
            return ResultAjax.success().add("result", "添加成功");
        }
        return ResultAjax.success().add("result", "添加失败");
    }

    @RequestMapping("/updateNews")
    @CheckPermission({"1"})
    public ResultAjax updateNews(@RequestBody Map<String, String> params) {
        String title = params.get("title");
        String content = params.get("content");
        int id = Integer.parseInt(params.get("id"));
        News news = new News();
        news.setTitle(title);
        news.setContent(content);
        news.setId(id);
        if (newsService.updateNews(news) > 0) {
            return ResultAjax.success().add("result", "修改成功");
        }
        return ResultAjax.success().add("result", "修改失败");
    }


    @RequestMapping("/deleteNews")
    @CheckPermission({"1"})
    public ResultAjax deleteNews(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        if (newsService.deleteNews(id) > 0) {
            return ResultAjax.success().add("result", "删除成功");
        }
        return ResultAjax.success().add("result", "删除失败");
    }


    @RequestMapping("/getNewsById")
    public ResultAjax geyNewsById(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        News news = newsService.getNewsById(id);
        return ResultAjax.success().add("news", news);
    }

    @RequestMapping("/checkTitle")
    public ResultAjax checkTitle(@RequestBody Map<String, String> params) {
        String title = params.get("title");
        if (newsService.getNewsByTitle(title) != null) {
            return ResultAjax.success().add("result", "exist");
        }
        return ResultAjax.success().add("result", "not exist");
    }

    @RequestMapping("/pageInfoNews")
    public ResultAjax getFrontFiveNews() {
        return ResultAjax.success().add("news", newsService.getFrontFiveNews());
    }


    @RequestMapping("/getNewsDetail")
    public ResultAjax getNewsDetail(@RequestBody Map<String, String> params) {
        int id = Integer.parseInt(params.get("id"));
        return ResultAjax.success().add("news", newsService.getNewsById(id));
    }


}
